define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'strategy/index' + location.search,
                    add_url: 'strategy/add',
                    edit_url: 'strategy/edit',
                    del_url: 'strategy/del',
                    multi_url: 'strategy/multi',
                    import_url: 'strategy/import',
                    table: 'strategy',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user.nickname', searchable:false,title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'user.mobile', title: __('User.mobile'), operate: 'LIKE'},
                        {field: 'num',searchable:false, title: __('Num')},
                        {field: 'lever_num',searchable:false, title: __('Lever_num')},
                        {field: 'no',searchable:false, title: __('No'), operate: 'LIKE'},
                        {field: 'all_no', title: __('All_no'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'buy_price',searchable:false, title: __('Buy_price'), operate:'BETWEEN'},
                        {field: 'sell_price',searchable:false, title: __('Sell_price'), operate:'BETWEEN'},
                        {field: 'income',searchable:false, title: __('Income'), operate:'BETWEEN'},
                        {field: 'income_ratio',searchable:false, title: __('Income_ratio'), operate:'BETWEEN'},
                        {field: 'earnest_money',searchable:false, title: __('Earnest_money'), operate:'BETWEEN'},
                        {field: 'status',searchable:false, title: __('Status'), searchList: {"1":__('持有'),"0":__('已平仓'),"3":__('亏损预警')}, formatter: Table.api.formatter.status,custom:{'3':'error'}},
						
                        {field: 'is_auto',searchable:false, title: __('Is_auto'), searchList: {"1":__('Is_auto 1'),"2":__('Is_auto 2')}, formatter: Table.api.formatter.normal},
                       
                        {field: 'buy_time',searchable:false, title: __('Buy_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'pay_time', searchable:false,title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operation',searchable:false, title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    title: __('编辑'),
                                    classname: 'btn btn-xs btn-succes btn-editone',
                                    icon: 'fa fa-pencil',
                                    url: 'strategy/edit',
                                    refresh: true,
                                }
                            ],
                            formatter: Table.api.formatter.buttons}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
			
			$('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
				
                var value = $(this).data("value");
				
                var options = table.bootstrapTable('getOptions');
				console.log(options)
					options.pageNumber = 1;
					options.queryParams = function (params) {
					  
						params.type = JSON.stringify(value);
						return params;
					};
					table.bootstrapTable('refresh', {});
					return false;
                
            });
			// 搜索
			$("form.form-commonsearch button[type='submit']").on("click", function (event) {
                   event.stopPropagation();
                   event.preventDefault();
                   var tab_search_value = $('.nav-tabs .active').data('value');
                var filter = {};
                var op = {};
                var commonsearch = $('.commonsearch-table');
                $("form.form-commonsearch .operate", commonsearch).each(function (i) {
                    var name = $(this).data("name");
                    var sym = $(this).is("select") ? $("option:selected", this).val() : $(this).val().toUpperCase();
                    var obj = $("[name='" + name + "']", commonsearch);
                    if (obj.size() == 0){
                        return true;
                    }
                    
                    var inputval = (typeof $("select[name='"+name+"']").val() != 'undefined') ? $("select[name='"+name+"']").val() : $("input[name='"+name+"']").val();
                    if ((typeof inputval == 'undefined') || inputval == '' || inputval == null || ($.isArray(inputval) && inputval.length == 0) && !sym.match(/null/i)) {
                        return true;
                    }
                    op[name] = sym;
                    filter[name] = inputval;
                });
                // console.log({op: op, filter: filter});
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    var customp = {};
                    customp.tab_search = tab_search_value;
                    params.customp = JSON.stringify(customp);
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
                table.bootstrapTable('refresh');
            });

               $("form.form-commonsearch button[type='reset']").on("click", function(event){
                   event.stopPropagation();
                   event.preventDefault();
                   var tab_search_value = $('.nav-tabs .active').data('value');
                var filter = {};
                var op = {};
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    var customp = {};
                    customp.tab_search = tab_search_value;
                    params.customp = JSON.stringify(customp);
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                }
                $("form.form-commonsearch")[0].reset();
                table.bootstrapTable('refresh');
               });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
          
    return Controller;
});
